package com.woniuxy.caradmin.controller;


import com.woniuxy.caradmin.entity.pojo.Permission;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.PermissionService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/selectAllPermission")
    @ApiOperation("获取所有权限")
    public R selectAllPermission(){
        List<Permission> permissions = permissionService.selectAllPermission();
        if(permissions!=null&&permissions.size()>0){
            return R.ok().data("permissions",permissions);
        }else {
            throw new CarException(ResultCode.PERISNULL, Constaint.PERISNULL);
        }
    }
    @ApiOperation("勾选菜单复选框")
    @GetMapping("/getChecked")
    public R getChecked(Integer rid){
        log.debug("****"+rid);
        Integer[] permissionIds = permissionService.selectPermissionsByRid(rid);
        return R.ok().data("permissionIds",permissionIds);
    }
    @ApiOperation("权限分配")
    @GetMapping("/assignRight")
    public R assignRight(Integer rid, Integer[] perIds){
        boolean flag = permissionService.assignRight(rid,perIds);
        if(flag){
            return R.ok().code(ResultCode.PERSUCCESS).message(Constaint.PERSUCCESS);
        }else {
            throw new CarException(ResultCode.PERFIAIL,Constaint.PERFIAIL);
        }
    }
}

